package com.zggk.lzz.xcbg;

import android.app.Activity;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.callback.StringCallback;
import com.zggk.lzz.api.MyApplication;
import com.zggk.lzz.api.Urls;
import com.zggk.lzz.callback.StringDialogCallback;
import com.zggk.lzz.gj.GJ2Bean;
import com.zggk.lzz.gj.GJBean;
import com.zggk.lzz.mvp.BasePresenterImpl;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * MVPPlugin
 *  邮箱 784787081@qq.com
 */

public class XcbgPresenter extends BasePresenterImpl<XcbgContract.View> implements XcbgContract.Presenter{
    @Override
    public void getData(String id, Activity activity) {
        OkGo.<String>post(Urls.JAVASERVER+"pms/rhms/lzzapp/getPatrolInfoById")
                .params("id", id)
                .params("token", "Bearer$" + MyApplication.spUtils.getString("Token",""))
                .execute(new StringDialogCallback(activity) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        XcbgMxBean xcbgMxBean = JSON.parseObject(response.body(),XcbgMxBean.class);
                        if (mView == null)
                            return;
                        if (xcbgMxBean.getCode().equals("200")){
                            mView.getDatas(xcbgMxBean.getData());
                        }else {
                            mView.onRequestError(xcbgMxBean.getMessage());
                        }
                    }
                });
    }
    @Override
    public void getLinDatas(String patrolId,Activity activity) {
        OkGo.<String>post(Urls.JAVASERVER+"pms/rhms/lzzapp/getGJByPatrolId")
                .params("patrolId", patrolId)
                .params("token", "Bearer$" + MyApplication.spUtils.getString("Token", ""))
                .execute(new StringDialogCallback(activity) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        GJBean gJBean = JSON.parseObject(response.body(), GJBean.class);
                        if (mView == null)
                            return;
                        if (gJBean.getCode().equals("200")) {
                            List<GJBean.DataBean> JWD= new ArrayList<>();
                            for (int i = 0; i < gJBean.getData().size(); i++) {
                                if (gJBean.getData().get(i).getJWData().getLat()!=null){
                                    JWD.add(gJBean.getData().get(i));
                                }
                            }
                            mView.getLinDatass(JWD);
                        } else {
                            mView.onRequestError(gJBean.getMessage());
                        }
                    }
                });
    }
    @Override
    public void getLinDatas2(String ID) {
        OkGo.<String>post(Urls.JAVASERVER+"pms/rhms/lzzapp/getRoadJW")
                .params("token", "Bearer$" + MyApplication.spUtils.getString("Token", ""))
                .params("patrolld", ID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        GJ2Bean gJ2Bean = JSON.parseObject(response.body(), GJ2Bean.class);
                        if (mView == null)
                            return;
                        if (gJ2Bean.getCode().equals("200")) {
                            mView.getLinDatass2(gJ2Bean.getData());
                        } else {
                            mView.onRequestError(gJ2Bean.getMessage());
                        }
                    }
                });
    }
}
